<!--
 * @Description: 
 * @Author: chenjiaxin
 * @Date: 2022-03-05 20:39:31
 * @LastEditTime: 2022-03-09 19:15:37
 * @FilePath: \super-unit\test\index4.html
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>index4-fill</title>
  </head>
  <body>
    <script>
      var data = {
        name: "",
        age: "",
        b: "",
        set: "",
        userList: [
          {
            id: "",
            name: "",
            age: ""
          },
          {
            id: "",
            name: "",
            age: ""
          }
        ],
        info: {
          adress: {
            area: "",
            qu: ""
          },
          userId: []
        },
        fu: ""
      };
      var apiData = {
        name: "cjx",
        age: 18,
        b: true,
        userList: [
          {
            id: "1",
            name: "kobi",
            age: 12
          },
          {
            id: "2",
            name: "jans",
            age: 23
          }
        ],
        info: {
          adress: {
            area: "北京",
            qu: "北京市"
          },
          userId: [1, 2, 3, 4]
        },
        fun: {
          name: "why"
        },
        set: new Set([111, 222, 333])
      };
      function fill(target, value) {
        if (value && !target) return value;
        if (typeof value !== "object") return value || target;
        if (Array.isArray(target) && Array.isArray(value)) {
          target = [];
          // console.log(target, value);
          target.push(...value);
          return target;
        } else {
          // object
          for (const key in target) {
            target[key] = fill(target[key], value[key]);
          }
          return target;
        }
      }
      let result = fill(data, apiData);
      console.log(data);
      console.log(result);
    </script>
  </body>
</html>
